package com.dfbz.demo;

import com.cloud.item.ItemApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
@SpringBootTest(classes = ItemApplication.class)
@RunWith(SpringRunner.class)
public class Demo02_DiscoveryClient {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Test
    public void test() {

        // 根据服务名获取实例
        List<ServiceInstance> instances = discoveryClient.getInstances("order-service");

        ServiceInstance instance = instances.get(0);

        System.out.println("服务实例id: "+instance.getInstanceId());
        System.out.println("服务名称: "+instance.getServiceId());
        System.out.println("主机ip: "+instance.getHost());
        System.out.println("端口: "+instance.getPort());
        System.out.println("协议: "+instance.getScheme());

        /*
        [
        	EurekaDiscoveryClient.EurekaServiceInstance@630febd7 instance = InstanceInfo
        	[
        	instanceId = 192.168.18.1:9001,
        	appName = ORDER-SERVICE,
        	hostName = 192.168.18.1,
        	status = UP,
        	ipAddr = 192.168.18.1,
        	port = 9001,
        	securePort = 443,
        	dataCenterInfo = com.netflix.appinfo.MyDataCenterInfo@45c2c396
        	]
         */
        System.out.println(instance);
    }


    @Test
    public void test1() {

        // 根据服务名获取实例
        List<ServiceInstance> instances = discoveryClient.getInstances("order-service");

        ServiceInstance instance = instances.get(0);

        System.out.println(instance);

        // 使用discoveryClient获取服务元数据进行服务消费(解耦)
        Map resultMap = restTemplate.getForObject("http://" + instance.getHost() + ":" + instance.getPort() + "/order/110", Map.class);

        System.out.println(resultMap);
    }


    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Test
    public void test2() {

        // 根据id获取服务实例
        ServiceInstance instance = loadBalancerClient.choose("order-service");

        System.out.println(instance);
        for (int i = 0; i < 10; i++) {
            Map resultMap = restTemplate.getForObject("http://" + instance.getHost() + ":" + instance.getPort() + "/order/110", Map.class);

            System.out.println(resultMap);
        }
    }

    @Test
    public void test3() {

        // 根据服务名获取实例
        List<ServiceInstance> instances = discoveryClient.getInstances("order-service");

        ServiceInstance instance = instances.get(0);

        System.out.println(instance);

        // 循环调用接口十次
        for (int i = 0; i < 10; i++) {

            // 使用服务名调用接口
            Map resultMap = restTemplate.getForObject("http://" + instance.getServiceId() + "/order/110", Map.class);

            System.out.println(resultMap);
        }

    }

    @Test
    public void test4() {

        // 获取服务实例
        ServiceInstance instance = loadBalancerClient.choose("order-service");

        // 循环调用接口十次
        for (int i = 0; i < 10; i++) {
            // 使用服务名调用接口
            Map resultMap = restTemplate.getForObject("http://" + instance.getServiceId() + "/order/110", Map.class);

            System.out.println(resultMap);
        }

    }


}
